package com.thinksns.sociax.t4.android.jipu;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.ProgressBar;

import com.thinksns.sociax.android.R;
import com.thinksns.sociax.component.CustomTitle;
import com.thinksns.sociax.component.LeftAndRightTitle;
import com.thinksns.sociax.t4.android.Thinksns;
import com.thinksns.sociax.t4.android.ThinksnsAbscractActivity;
import com.thinksns.sociax.t4.unit.UnitSociax;
import com.thinksns.sociax.thinksnsbase.activity.widget.LoadingView;

/**
 * @Describe 极铺网页WEb页
 * @Author zhiyi-hehuajun
 * @Date 2016/11/29
 */
public class JiPuShopActivity extends ThinksnsAbscractActivity {
    public static final String BUNDLE_ID = "id";
    private WebView wv_about_us;
    private Button mBtClose;
    private ProgressBar pb_bar;
    protected UnitSociax uint;
    private String URL = "";
    private String id = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initView();
        initListener();
        initData();
    }

    private void initListener() {
        mBtClose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    private void initData() {
        id = getIntent().getStringExtra(BUNDLE_ID);
        if (!TextUtils.isEmpty(id)) {
            URL = String.format(JiPuConfig.JIPU_SHOP_DETAIL_HEAD, id, Thinksns.getMy().getToken(), Thinksns.getMy().getSecretToken(), Thinksns.getMy().getUid() );
        } else {
            URL = String.format(JiPuConfig.JIPU_HOME_URL, Thinksns.getMy().getToken(), Thinksns.getMy().getSecretToken(), Thinksns.getMy().getUid());
        }

        uint = new UnitSociax(this);
        wv_about_us.setHorizontalScrollBarEnabled(false);    //设置滑动条水平不显示
        wv_about_us.setVerticalScrollBarEnabled(false);    //设置滑动条垂直不显示
        pb_bar = (ProgressBar) this.findViewById(R.id.pb_bar);
        wv_about_us.setWebChromeClient(new WebChromeClient() {

            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                if (newProgress == 100) {
                    pb_bar.setVisibility(View.GONE);
                } else {
                    if (View.GONE == pb_bar.getVisibility()) {
                        pb_bar.setVisibility(View.VISIBLE);
                    }
                    pb_bar.setProgress(newProgress);
                }
                super.onProgressChanged(view, newProgress);
            }
        });
        wv_about_us.getSettings().setJavaScriptEnabled(true);
        wv_about_us.loadUrl(URL);
        wv_about_us.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }
        });
    }

    private void initView() {
        loadingView = (LoadingView) findViewById(LoadingView.ID);
        wv_about_us = (WebView) findViewById(R.id.wv_about_us);
        mBtClose = (Button) findViewById(R.id.bt_close);


    }

    @Override
    public String getTitleCenter() {
        return getResources().getString(R.string.e_jipu);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_jipu_shop;
    }

    @Override
    protected CustomTitle setCustomTitle() {
        return new LeftAndRightTitle(R.drawable.img_back,"",this);
    }

    /**
     * 覆盖系统的回退键
     *
     * @param keyCode
     * @param event
     * @return
     */
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && wv_about_us.canGoBack()) {
            wv_about_us.goBack();
            return true;
        }

        return super.onKeyDown(keyCode, event);
    }


}
